﻿using System;
using System.Data;
using System.Collections.Generic;
using OnlineManagement.Model;
using OnlineManagement.DALFactory;
using OnlineManagement.IDAL;
namespace OnlineManagement.BLL
{
	/// <summary>
	/// Sow
	/// </summary>
	public partial class Sow
	{
		private readonly ISow dal=DataAccess.CreateSow();
		public Sow()
		{}
		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid SowID)
		{
			return dal.Exists(SowID);
		}

		
        /// <summary>
        /// 使用存储过程增加一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="returnValue">存储过程返回的信息</param>
        /// <returns></returns>
        public bool Add(OnlineManagement.Model.Sow model, out string returnValue, out string sowIdStr)
        {
            return dal.Add(model, out returnValue,out sowIdStr);
        }
		
        /// <summary>
        /// 使用存储过程更新一条数据
        /// </summary>
        public bool Update(OnlineManagement.Model.Sow model,out string message) {
            return dal.Update(model, out message);
        }
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid SowID)
		{
			
			return dal.Delete(SowID);
		}
        public bool Delete(Guid sowId,out string message) {
            return dal.Delete(sowId, out message);
        }
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string SowIDlist )
		{
			return dal.DeleteList(SowIDlist );
		}
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OnlineManagement.Model.Sow GetModel(Guid SowID)
		{
			
			return dal.GetModel(SowID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public OnlineManagement.Model.Sow GetModelByCache(Guid SowID)
		{
			
			string CacheKey = "SowModel-" + SowID;
			object objModel = OnlineManagement.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(SowID);
					if (objModel != null)
					{
						int ModelCache = OnlineManagement.Common.ConfigHelper.GetConfigInt("ModelCache");
						OnlineManagement.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (OnlineManagement.Model.Sow)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<OnlineManagement.Model.Sow> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<OnlineManagement.Model.Sow> DataTableToList(DataTable dt)
		{
			List<OnlineManagement.Model.Sow> modelList = new List<OnlineManagement.Model.Sow>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				OnlineManagement.Model.Sow model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
        public DataSet GetSowsByProc(int sowId, int currentPage) {
            return dal.GetSowsByProc(sowId, currentPage);
        }

        public DataSet GetHybridizationSow(int hoggeryId) {
            return dal.GetHybridizationSow(hoggeryId);
        }
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		

		#endregion  BasicMethod

		#region  ExtensionMethod

        /// <summary>
        ///  get sows info for SowSetting page
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <returns></returns>
        public DataSet GetSowSettingsInfo(int hoggeryId) {
            return  dal.GetSowSettingsInfo(hoggeryId);
        }


       public DataSet GetSowActions() {
           DataSet ds = null;
           object obj = Common.DataCache.GetCache("sowAction");
           if (obj != null && obj is DataSet)
           {
               ds = obj as DataSet;
           }
           else {
               ds = dal.GetSowActions();
               Common.DataCache.SetNoAbsoluteExpirationCache("sowAction",ds,TimeSpan.FromMinutes(5));
           }
           return ds;
       }
       public List<Model.SowOperations> GetSowOperations() {
           List<Model.SowOperations> lists;
           string cacheName = "sowOperations";
           object obj = Common.DataCache.GetCache(cacheName);
           if (obj != null && obj is List<Model.SowOperations>)
           {
               lists = obj as List<Model.SowOperations>;
           }
           else {
               lists = dal.GetSowOperations();
               Common.DataCache.SetCache(cacheName, lists);
           }
           return lists;
       }

        public DataSet GetSowDataPaging(int hoggeryId, int pageSize, int currentPage, string whereStr, string orderByStr, out int rCount) {
            try
            {
                return dal.GetSowDataPaging(hoggeryId, pageSize, currentPage, whereStr, orderByStr, out rCount);
            }catch(Exception ex){
                rCount = 0;
                return null;
            }
       }

        /// <summary>
        /// get sow basic infomation for every sow operation
        /// BitID,CustomizeID,TypeName
        /// </summary>
        /// <param name="sowID"></param>
        /// <param name="hoggeryId"></param>
        /// <returns>BitID,CustomizeID,TypeName dataset</returns>
        public DataSet GetSowBasicInfo(Guid sowID, int hoggeryId) {

            DataSet ds = dal.GetSowBasicInfo(sowID, hoggeryId); ;
            //remove cache
            //object obj= Common.DataCache.GetCache("SowBasicInfo");
            //if (obj != null && obj is DataSet)
            //{
            //    ds = obj as DataSet;
            //}
            //else { 
            //    ds= dal.GetSowBasicInfo(sowID, hoggeryId);
            //    Common.DataCache.SetCache("SowBasicInfo", ds);
            //}
            return ds;
        }
		#endregion  ExtensionMethod
	}
}

